<template>
	<view>
		<u-sticky><u-tabs :list="list" :is-scroll="false" :current="current" @change="sectionChange"></u-tabs></u-sticky>
		<view class="content wrap" v-if="current == 0">
			<u-form :model="form2" ref="uForm"  :rules="rules">
				<u-form-item label="出库日期*" label-width="180" left-icon="calendar"><u-input v-model="form2.times" placeholder="请选择出库日期" disabled="true" disabledColor="#b9babc"/></u-form-item>
				
				<u-form-item label="商品*" label-width="150">
					<u-radio-group v-model="form2.goodsName">
						<u-radio 
							@change="radioChange2" 
							v-for="(item, index) in glist" :key="index" 
							:name="item.name"
						>
						{{item.name}}
						</u-radio>
					</u-radio-group>
				</u-form-item>
				
				<u-form-item label="重量*" label-width="180" prop="weight"><u-input v-model="form2.weight" type="digit" placeholder="请输入重量"/></u-form-item>
				
				<u-form-item label="客户名称*" label-width="180" left-icon="home" prop="customerName">
					<u-input v-model="form2.customerName" placeholder="请输入客户名称"/>
					<u-icon @click="searchCustomer()" slot="right" name="search" size="60"></u-icon>
				</u-form-item>
				
				<u-form-item label="地址" label-width="180"><u-input v-model="form2.customerAddress" placeholder="请输入地址"/></u-form-item>
				
				<u-form-item label="联系人" label-width="180"><u-input v-model="form2.contacts" placeholder="请输入联系人"/></u-form-item>
				
				<u-form-item label="联系方式" label-width="180" left-icon="phone"><u-input v-model="form2.tel" placeholder="请输入联系方式"/></u-form-item>
				
				<u-form-item label="运输车辆*" label-width="180" left-icon="car" prop="carNumber"><u-input v-model="form2.carNumber" placeholder="请输入运输车辆"/></u-form-item>
				
				<u-form-item label="司机" label-width="180" left-icon="account"><u-input v-model="form2.driverName" placeholder="请输入司机"/></u-form-item>
				
				<u-form-item label="司机电话" label-width="180" left-icon="phone"><u-input v-model="form2.driverTel" placeholder="请输入司机电话"/></u-form-item>
				
				<u-form-item label="成本价" label-width="180" left-icon="rmb"><u-input v-model="form2.costPrice" placeholder="请输入成本价"/></u-form-item>
				
				<u-form-item label="销售价" label-width="180" left-icon="rmb"><u-input v-model="form2.salesPrice" placeholder="请输入销售价"/></u-form-item>
				
			</u-form>
			<u-button @click="submit2">提交</u-button>
		</view>
		
		<u-popup v-model="popupShow2" mode="center" closeable width="550rpx" height="600rpx" border-radius="10">
			<view class=" u-p-r-30 u-p-l-30 u-m-t-20" style="margin-top: 40rpx;">
				<u-search placeholder="输入客户名称搜索" :clearabled="true" v-model="keyword" border-color="#606266" @custom="searchCustomer" @search="searchCustomer"></u-search>
			</view>
			<view class="wrap">
				<u-row v-for="(item,index) in clist" :key="index" @click="xuanzeCustomer(item)">
					<u-col span="12" text-align="center">
						<view class="u-m-t-25 weikaishi4 lanse">
							<span>{{item.customerName}}</span>
						</view>
					</u-col>
				</u-row>
			</view>
		</u-popup>
		
		<view>
			<u-toast ref="uToast" />
		</view>
		<view class="" v-if="current == 1">
			<u-card padding="16" v-for="(item,index) in dataList2" :key="index">
				<view class="" slot="head">
					<view class="wrap">
						<u-row>
							<u-col span="9" text-align="left">
								<view class="u-line-1 u-m-t-2 weikaishi3">
									<span>出库单号：{{item.outNumber}}</span>
								</view>
							</u-col>
							<u-col span="3" text-align="left">
								<view class="u-line-1 u-m-t-2 weikaishi3">
									<span>{{item.states}}</span>
								</view>
							</u-col>
						</u-row>
						<u-row>
							<u-col span="6" text-align="left">
								<view class="u-line-1 u-m-t-2 weikaishi">
									<span>时间：{{item.times}}</span>
								</view>
							</u-col>
							<u-col span="6" text-align="right">
								<view class="u-line-1 u-m-t-2 weikaishi">
									<span>销售渠道：{{item.channelName}}</span>
								</view>
							</u-col>
						</u-row>
					</view>
				</view>
				<view class="" slot="body">
					<view class="wrap">
						<u-row>
							<u-col span="6" text-align="left">
								<view class="u-line-1 u-m-t-4">
									<span>出库仓库：</span><span>{{item.warehouseName}}</span>
								</view>
							</u-col>
							<u-col span="6" text-align="left">
								<view class="u-line-1 u-m-t-4">
									<span>出库库位：</span><span>{{item.positionNumber}}</span>
								</view>
							</u-col>
							<u-col span="6" text-align="left">
								<view class="u-line-1 u-m-t-4">
									<span>客户：</span><span>{{item.customerName}}</span>
								</view>
							</u-col>
							<u-col span="6" text-align="left">
								<view class="u-line-1 u-m-t-4">
									<span>客户地址：</span><span>{{item.customerAddress}}</span>
								</view>
							</u-col>
							<u-col span="6" text-align="left">
								<view class="u-line-1 u-m-t-4">
									<span>联系人：</span><span>{{item.contacts}}</span>
								</view>
							</u-col>
							<u-col span="6" text-align="left">
								<view class="u-line-1 u-m-t-4">
									<span>联系电话：</span><span>{{item.tel}}</span>
								</view>
							</u-col>
							<u-col span="6" text-align="left">
								<view class="u-line-1 u-m-t-4">
									<span>结算方式：</span><span>{{item.settlementMethod}}</span>
								</view>
							</u-col>
							<u-col span="6" text-align="left">
								<view class="u-line-1 u-m-t-4">
									<span>销售平台：</span><span>{{item.channelPlatform}}</span>
								</view>
							</u-col>
							<u-col span="6" text-align="left">
								<view class="u-line-1 u-m-t-4">
									<span>发货方式：</span><span>{{item.transportModel}}</span>
								</view>
							</u-col>
							<u-col span="6" text-align="left">
								<view class="u-line-1 u-m-t-4">
									<span>运输车辆：</span><span>{{item.carNumber}}</span>
								</view>
							</u-col>
							<u-col span="6" text-align="left">
								<view class="u-line-1 u-m-t-4">
									<span>司机：</span><span>{{item.driverName}}</span>
								</view>
							</u-col>
							<u-col span="6" text-align="left">
								<view class="u-line-1 u-m-t-4">
									<span>司机电话：</span><span>{{item.driverTel}}</span>
								</view>
							</u-col>
							<u-col span="6" text-align="left">
								<view class="u-line-1 u-m-t-4">
									<span>快递公司：</span><span>{{item.courierCompany}}</span>
								</view>
							</u-col>
							<u-col span="6" text-align="left">
								<view class="u-line-1 u-m-t-4">
									<span>快递单号：</span><span>{{item.courierNumber}}</span>
								</view>
							</u-col>
							<u-col span="6" text-align="left">
								<view class="u-line-1 u-m-t-4">
									<span>成本价：</span><span>{{item.costPrice}}</span>
								</view>
							</u-col>
							<u-col span="6" text-align="left">
								<view class="u-line-1 u-m-t-4">
									<span>提成：</span><span>{{item.commission}}</span>
								</view>
							</u-col>
							<u-col span="6" text-align="left">
								<view class="u-line-1 u-m-t-4">
									<span>销售价：</span><span>{{item.salesPrice}}</span>
								</view>
							</u-col>
						</u-row>
					</view>
				</view>
				<view class="foot-butten" slot="foot">
					<view :class="flex">
						<u-col span="6" text-align="center" @click="showDetailed(item.outNumber)">
							<view  class="u-m-10">
								商品明细
							</view>
						</u-col>
						<u-col span="6" text-align="center" @click="revoke(item.id)" v-if="item.states == '已出'">
							<view class="u-m-10 yizuofei">
								撤销
							</view>
						</u-col>
					</view>
				</view>
			</u-card>
			<u-popup v-model="popupShow" mode="center" closeable width="680rpx" height="720rpx" border-radius="10">
				<view class="u-text-center u-m-t-20 weikaishi3"><u-icon name="order" size="30" labelSize="15px" labelColor="#000000" label="出库明细"></u-icon></view>
				<u-card padding="16">
					<view class="" slot="head">
						<view class="wrap">
							<u-row>
								<u-col span="12" text-align="center">
									<view class="weikaishi3 lanse">
										<span v-html="'出库单号'+outNumber"></span>
									</view>
								</u-col>
							</u-row>
						</view>
					</view>
					<view class="" slot="body">
						<view class="wrap">
							<u-row >
								<u-col span="3" text-align="center">
									<view class="u-line-1 u-m-t-10 weikaishi">
										<span>库位</span>
									</view>
								</u-col>
								<u-col span="3" text-align="center">
									<view class="u-line-1 u-m-t-10 weikaishi">
										<span>商品</span>
									</view>
								</u-col>
								<u-col span="3" text-align="center">
									<view class="u-line-1 u-m-t-10 weikaishi">
										<span>重量</span>
									</view>
								</u-col>
								<u-col span="3" text-align="center">
									<view class="u-line-1 u-m-t-10 weikaishi">
										<span>生产日期</span>
									</view>
								</u-col>
							</u-row>
							<u-row v-for="(item1,index1) in dataList3" :key="index1">
								<u-col span="3" text-align="center">
									<view class="u-m-t-15 weikaishi4">
										<span>{{item1.positionNumber}}</span>
									</view>
								</u-col>
								<u-col span="3" text-align="center">
									<view class=" u-m-t-10 weikaishi huanhang">
										<span>{{item1.goodsName}}</span>
									</view>
								</u-col>
								<u-col span="3" text-align="center">
									<view class="u-m-t-15 weikaishi huanhang">
										<span>{{item1.weight}}</span>
									</view>
								</u-col>
								<u-col span="3" text-align="center">
									<view class="u-m-t-15 weikaishi huanhang">
										<span>{{item1.producetime}}</span>
									</view>
								</u-col>
							</u-row>
						</view>
					</view>
				</u-card>
			</u-popup>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				flex:'',
				list: [
					{
						name: '处置出库'
					}, 
					{
						name: '处置查询'
					}
				],
				current: 0,
				dataList2: [],
				dataList3: [],
			    pageNo: 1,
			    hasMore: true,
			    orderCount: 0,
				form2:{
				    times:'',
				    weight:"",
					goodsName:'',
				    customerName:"",
					customerAddress:"",
				    contacts:"",
				    tel:"",
				    carNumber:"",
				    driverName:"",
				    driverTel:"",
				    costPrice:"",
				    salesPrice:"",
				},
				glist:[],
				clist:[],
			    rules: {
					weight: [
						{ 
							required: true,
							message: '请输入重量', 
							trigger: 'blur',
						}
					],
					customerName: [
						{ 
							required: true,
							message: '请输入客户名称', 
							trigger: 'blur',
						}
					],
					carNumber: [
						{ 
							required: true,
							message: '请输入运输车辆', 
							trigger: 'blur',
						}
					],
				},
				popupShow:false,
				popupShow2:false,
				outNumber:'',
				keyword:'',
			}
		},
		onPullDownRefresh() {
			this.refresh()
			uni.stopPullDownRefresh()
		},
		onReachBottom() {
			if(this.current == 1){
				uni.showLoading({
					title: '加载中'
				})
				if (this.hasMore) {
					this.pageNo++ 
					setTimeout(() => {
						this.loadOrder({
						}).then(res => {})
						uni.hideLoading()
					}, 500)
				} else {
					setTimeout(() => {
						uni.hideLoading()
						uni.showToast({
							icon: 'none',
							title: '已经到低了哦',
							duration: 2000
						});
					}, 500)
				}
			}
		},
		onLoad(open) {
			//#ifdef H5 || APP-PLUS
			this.flex = 'u-flex'
			//#endif
			this.refresh()
			
			let year = new Date().getFullYear();//年
			let month = new Date().getMonth() + 1;//注意！月份是从0月开始获取的，所以要+1;
			let day = new Date().getDate();//日
			this.form2.times =  year + '-' + (month >= 10 ? month : '0' + month) + '-' + (day >= 10 ? day : '0' + day);
			
			this.$http.post('/rest/fruits/findgoodslist',{
			},{
				params: {},
				header: {
					'Authorization' :uni.getStorageSync('token'),
				}
			}).then(res => {
				if(res.data.data.length>0){
					this.form2.goodsName = res.data.data[0].goodsName
					for(let item1 of res.data.data) {
						let obj = {}
						obj.name = item1.goodsName
						this.glist.push(obj)
					}
				}
			})
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			xuanzeCustomer(item){
				this.form2.customerName = item.customerName
				this.form2.customerAddress = item.customerAddress
				this.form2.contacts = item.contacts
				this.form2.tel = item.tel
				this.popupShow2 = false
				this.keyword = ''
			},
			searchCustomer(){
				this.$http.post('/rest/fruits/findfruitsBasicCustomer',{
				},{
					params: {
						customerName:this.keyword
					},
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
					let arr = []
					let inta = 0
					for(let item1 of res.data.data) {
						if(inta != 10){
							arr.push(item1)
						} else {
							break
						}
						inta++
					}
					this.clist = arr
					this.popupShow2 = true
				})
			},
			revoke(id) {
				uni.showModal({
					content: '确定撤销吗？',
					success: (res) => {
						if (res.confirm) {
							uni.showLoading({
								title: '正在提交'
							})
							this.$http.post('/rest/fruits/revoke_cz_out',{
							},{
								params: {id:id},
								header: {
									'Authorization' :uni.getStorageSync('token'),
								}
							}).then(res => {
								uni.hideLoading()
								if(res.data.code == 200) {
									this.$refs.uToast.show({
										title: '操作成功',
										type: 'success'
									})
									this.refresh()
								} else {
									this.$refs.uToast.show({
										title: res.data.message,
										type: 'error'
									})
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			showDetailed(outNumber){
				this.outNumber = outNumber
				this.$http.post('/rest/fruits/findzhifacangOutDetailedlist',{
				},{
					params: {outNumber:outNumber},
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
					this.dataList3 = res.data.data
					this.popupShow = true
				})
			},
			sectionChange(e){
				this.current = e
				this.refresh()
			},
			// 选中某个单选框时，由radio时触发
			radioChange2(e) {
				console.log(e);
			},
			submit2() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						console.log('验证通过');
						if(typeof this.form2.goodsName == 'undefined' || this.form2.goodsName == null || this.form2.goodsName == ""){
							uni.showToast({
								title: "请选择商品",
								icon: 'none'
							})
							return
						}
						uni.showLoading({
							title: '正在提交'
						})
						this.$http.post('/rest/fruits/addItemOutDefectiveproduct',{
						},{
							params: this.form2,
							header: {
								'Authorization' :uni.getStorageSync('token'),
							}
						}).then(res => {
							uni.hideLoading()
							if(res.data.code == 200) {
								this.form2.weight = ''
								this.form2.customerName = ''
								this.form2.customerAddress = ''
								this.form2.contacts = ''
								this.form2.tel = ''
								this.form2.carNumber = ''
								this.form2.driverName = ''
								this.form2.driverTel = ''
								this.form2.costPrice = ''
								this.form2.salesPrice = ''
								this.$refs.uToast.show({
									title: '操作成功',
									type: 'success'
								})
							} else {
								this.$refs.uToast.show({
									title: res.data.message,
									type: 'error'
								})
							}
						})
					} else {
						console.log('验证失败');
					}
				});
			},
			refresh(){
				uni.showLoading({
					title: '加载中...'
				})
				if(this.current == 1){
					this.pageNo = 1
					this.hasMore = true
					this.loadOrder({
						warehouseName:'次果仓'
					}).then(res => {
					})
				}
				uni.hideLoading()
			},
			loadOrder(param = {}) {
				return new Promise((resolve, reject) => {
					param = Object.assign(param, {
						page: this.pageNo,
						limit: 10
					})
					this.$http.post('/rest/fruits/findzhifacangOutPagelist',{
					},{
						params: param,
						header: {
							'Authorization' :uni.getStorageSync('token'),
						}
					}).then(res => {
						if (this.pageNo == 1) {
							this.dataList2 = res.data.data
						} else {
							this.dataList2 = this.dataList2.concat(res.data.data)
						}
						this.orderCount = res.data.count
						this.hasMore = this.pageNo * 10 < res.data.count
						resolve(res)
					})
				})
			}
		}
	}
</script>
<style lang="scss">
	page{
			background-color: #ffffff;
	}
	
	.wrap {
		padding: 30rpx;
	}
	.weikaishi2{
		font-size: 36rpx;
	}
	.weikaishi3{
		font-size: 32rpx;
	}
	.huanhang{
		word-break:break-all;
	}
</style>
